package com.cn.aam.checaiduo.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.back.BackHandlerHelper;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.ui.useraccess.LoginActivity;
import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * monker on 2017/5/11.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected ViewGroup WMContent;//DecorView  所有xml生成的View的父View  详情了解 @android的窗口机制
    protected Context mContext;
    private Unbinder unbinder;

    protected CompositeDisposable disposableList = new CompositeDisposable();

    public void addDisposable(Disposable d) {
        disposableList.add(d);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutId());
        mContext = this;
        unbinder = ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        //**********主视图*********
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//请求为竖直屏幕
        WMContent = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
        PushAgent.getInstance(this).onAppStart();
        onGenerate();
        initView();
    }

    /**
     * 获取布局
     *
     * @return
     */
    protected abstract int layoutId();

    /**
     * 界面初始化
     */
    protected void onGenerate() {
    }

    /**
     * 初始化界面视图
     */
    protected void initView() {
    }


    /**
     * 获取控件的方法
     */
    public <T extends View> T myFindViewsById(int viewId) {
        View view = findViewById(viewId);
        return (T) view;
    }


    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            finish();
        }
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }


    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    /**
     * 需要登录
     */
    public boolean needLogin() {
        if (!hasLogin() || !AppData.isGestureVerify) {
            launchLoginPage();
            return true;
        }
        return false;
    }


    protected boolean hasLogin() {

        UserCenter userCenter = UserCenter.getInstance();
        if (userCenter.isUserLogin()) {
            return true;
        }
        return false;
    }


    public void goLogin() {
        if (hasLogin())
            UserCenter.getInstance().logOut();
        launchLoginPage();
    }

    private void launchLoginPage() {
        startActivityForResult(LoginActivity.newInstance(mContext), Const.INTENT_REQUEST_LOGIN);
        overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.noting);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        disposableList.clear();
    }
}
